package com.threeox.commonlibrary.ui.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.view.inter.titlebar.ITitleBarView;
import com.threeox.commonlibrary.ui.view.inter.titlebar.OnTopBarListener;
import com.threeox.utillibrary.util.res.LayoutHelper;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 *
 * @ClassName: CommonTitleBarView
 *
 * @Description: TODO(类库的TopBar)
 *
 * @author 赵屈犇
 *
 * @date 2016-9-7 下午4:47:53
 *
 */
@SuppressLint("NewApi")
public class CommonTitleBarView extends LinearLayout implements OnClickListener, ITitleBarView {

	private Context mContext;
	private int rightIcon = R.mipmap.ic_launcher;
	private int leftIcon = R.mipmap.ic_bar_back;
	protected LinearLayout leftLayout, centerLayout, rightLayout;
	protected ProgressBar progress;
	protected ImageView leftIv, rightIv;
	protected TextView centerTv;
	private OnTopBarListener onTopbarListener = null;
	protected RelativeLayout tbLayout;

	public enum TopBarStyle {
		showAll, showLeft, showRight, showCenter, displayCenter, hideRight, hideAll
	}

	public enum LayoutStyle {
		left, right, center
	}

	public CommonTitleBarView(Context context) {
		this(context, null);
	}

	public CommonTitleBarView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

    public CommonTitleBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		initView();
		initEvent();
	}

	/**
	 * 初始化方法
	 *
	 * @param tbs
	 */
	public CommonTitleBarView init(TopBarStyle tbs, CharSequence title) {
		switch (tbs) {
			case showLeft:
				rightIv.setVisibility(View.INVISIBLE);
				rightLayout.setVisibility(View.INVISIBLE);
				break;
			case showRight:
				leftIv.setVisibility(View.INVISIBLE);
				leftLayout.setVisibility(View.INVISIBLE);
				break;
			case showCenter:
				rightIv.setVisibility(View.GONE);
				leftIv.setVisibility(View.GONE);
				break;
			case displayCenter:
				centerTv.setVisibility(View.GONE);
				break;
			case hideRight:
				rightIv.setVisibility(View.GONE);
				rightLayout.setVisibility(View.GONE);
				break;
			case hideAll:
				leftIv.setVisibility(View.GONE);
				leftLayout.setVisibility(View.GONE);
				rightIv.setVisibility(View.GONE);
				rightLayout.setVisibility(View.GONE);
				break;
		}
		setTitle(title);
		return this;
	}

	/**
	 * 初始化View
	 */
	protected void initView() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View view = inflater.inflate(R.layout.view_top_bar, this);
		tbLayout = (RelativeLayout) view.findViewById(R.id.topbar_layout);
		leftLayout = (LinearLayout) view.findViewById(R.id.left_layout); /** 左边布局 */
		rightLayout = (LinearLayout) view.findViewById(R.id.right_layout); /** 右边布局 */
		centerLayout = (LinearLayout) view.findViewById(R.id.center_layout); /** 中间布局 */
		progress = (ProgressBar) view.findViewById(R.id.topbar_progress); /** 旋转动画 */
		leftIv = (ImageView) view.findViewById(R.id.left_iv); /** 左边图片 */
		rightIv = (ImageView) view.findViewById(R.id.right_iv); /** 右边图片 */
		centerTv = (TextView) view.findViewById(R.id.center_text); /** 中间文字 */
	}

	public void setBackground(int resid) {
		tbLayout.setBackgroundResource(resid);
	}

	/**
	 * 设置prog
	 *
	 * @param msg
	 * @param visibliy
	 */
	public void setProg(String msg, int visibliy) {
		if (msg != null) {
			centerTv.setText(msg);
		}
		progress.setVisibility(visibliy);
	}

	/**
	 * 设置prog
	 *
	 * @param visibliy
	 */
	public void setProg(int visibliy) {
		progress.setVisibility(visibliy);
	}

	/**
	 * 初始化点击事件
	 */
	protected void initEvent() {
		leftLayout.setOnClickListener(this);
		rightLayout.setOnClickListener(this);
		centerLayout.setOnClickListener(this);
	}

	/**
	 * 得到标题名称
	 *
	 * @return
	 */
	public String getTitleName() {
		return centerTv.getText().toString();
	}

	/**
	 * 设置中间文字
	 *
	 * @param text
	 */
	@Override
	public void setTitle(CharSequence text) {
		if (text != null) {
			centerTv.setText(text);
			centerTv.setVisibility(View.VISIBLE);
		} else {
			centerTv.setVisibility(GONE);
		}
	}

	/**
	 * 设置左边图片
	 *
	 * @param leftIcon
	 */
	@Override
	public void setLeftIcon(int leftIcon) {
		this.leftIcon = leftIcon;
		leftIv.setImageResource(leftIcon);
		leftIv.setVisibility(View.VISIBLE);
		leftLayout.setVisibility(View.VISIBLE);
	}

	/**
	 * 设置右边图片
	 *
	 * @param rightIcon
	 */
	@Override
	public void setRightIcon(int rightIcon) {
		this.rightIcon = rightIcon;
		rightIv.setImageResource(rightIcon);
		rightIv.setVisibility(View.VISIBLE);
		rightLayout.setVisibility(View.VISIBLE);
	}

	@Override
	public void setAlpha(int alpha) {
		tbLayout.getBackground().setAlpha(alpha);
        if (alpha == 255) {
            leftLayout.getBackground().setAlpha(alpha);
            rightLayout.getBackground().setAlpha(alpha);
        } else {
            leftLayout.getBackground().setAlpha(0);
            rightLayout.getBackground().setAlpha(0);
        }
	}

	@Override
	public void setTitleBarColor(int color) {
		tbLayout.setBackgroundColor(getContext().getResources().getColor(color));
	}

    @Override
    public void setTitleBarTextColor(int color) {
        centerTv.setTextColor(getContext().getResources().getColor(color));
    }

    @Override
    public void setLeftBgResource(int resource) {
        leftLayout.setBackgroundResource(resource);
    }

    @Override
    public void setRightBgResource(int resource) {
        rightLayout.setBackgroundResource(resource);

    }

    /**
	 * 设置布局
	 *
	 * @param ls
	 */
	@SuppressWarnings("unchecked")
	public <T> T setLayout(int layoutId, LayoutStyle ls) {
		ViewGroup view = null;
		switch (ls) {
			case left:
				leftIv.setVisibility(GONE);
				leftLayout.setVisibility(VISIBLE);
				view = leftLayout;
				break;
			case right:
				rightIv.setVisibility(GONE);
				rightLayout.setVisibility(VISIBLE);
				view = rightLayout;
				break;
			case center:
				centerTv.setVisibility(GONE);
				centerLayout.setVisibility(VISIBLE);
				view = centerLayout;
				break;
		}
		return (T) LayoutUtils.inflate(mContext, layoutId, view);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.left_layout) {
            if (onTopbarListener != null) {
                if (onTopbarListener.onLeftLayoutClick(leftIv)) {
                    if (leftIcon == R.mipmap.ic_bar_back) {
						((Activity) mContext).finish();
					}
                }
            } else {
                if (leftIcon == R.mipmap.ic_bar_back) {
					((Activity) mContext).finish();
				}
            }
		} else if (id == R.id.right_layout) {
			if (onTopbarListener != null) {
				onTopbarListener.onRightLayoutClick(rightIv);
			}
		} else if (id == R.id.center_layout) {
			if (onTopbarListener != null) {
				onTopbarListener.onCenterLayoutClick(v);
			}
		}
	}

	@Override
	public void setOnTopbarListener(OnTopBarListener onTopbarListener) {
		this.onTopbarListener = onTopbarListener;
	}

	/**
	 * 添加居右的布局
	 *
	 * @param layoutId
	 */
	public void addRightLayout(int layoutId) {
        View view = LayoutUtils.inflate(mContext, layoutId);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.LEFT_OF, R.id.right_layout);
        tbLayout.addView(view, params);
    }

	public ProgressBar getProgressView() {
		return progress;
	}

	@Override
	public <T extends View> T getCenterLayout() {
		return (T) centerLayout;
	}

	@Override
	public <T extends View> T getLeftLayout() {
		return (T) leftLayout;
	}

	@Override
	public <T extends View> T getRightLayout() {
		return (T) rightLayout;
	}

	public TextView getCenterText() {
		return centerTv;
	}

	public ImageView getLeftIv() {
		return leftIv;
	}

	public ImageView getRightIv() {
		return rightIv;
	}

}
